/*دریافت استان‌ها و اضافه کردن کلاس‌ به استان های دارای نماینده*/

/*******php*******/
/*ارسال اطلاعات استان‌های با نماینده از سرور*/
function sigmaweb_get_provinces_with_representatives() {
    global $wpdb;

    $results = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = 'sigmaweb_province'");

    wp_send_json_success($results);
}

add_action('wp_ajax_sigmaweb_get_provinces_with_representatives', 'sigmaweb_get_provinces_with_representatives');
add_action('wp_ajax_nopriv_sigmaweb_get_provinces_with_representatives', 'sigmaweb_get_provinces_with_representatives');


/*******script*******/
/*دریافت استان‌ها و اضافه کردن کلاس‌ها*/
document.addEventListener('DOMContentLoaded', function () {
  // دریافت استان‌های با نماینده از سرور
  fetch('/wp-admin/admin-ajax.php?action=sigmaweb_get_provinces_with_representatives')
    .then(response => response.json())
    .then(data => {
      if (data.success) {
        const provincesWithRepresentatives = data.data;

        // انتخاب تمام مسیرهای نقشه
        const cityPaths = document.querySelectorAll('path.sigmaweb-iran-city-map');
        
        // اضافه کردن کلاس به path های مربوطه
        cityPaths.forEach(function (path) {
          const province = path.dataset.province;
          if (provincesWithRepresentatives.includes(province)) {
            path.classList.add('has-representative');
          }
        });

        // انتخاب تمام دکمه‌ها
        const cityButtons = document.querySelectorAll('.sigmaweb-iran-city-btn');
        
        // اضافه کردن کلاس به دکمه‌های مربوطه
        cityButtons.forEach(function (btn) {
          const province = btn.dataset.province;
          if (provincesWithRepresentatives.includes(province)) {
            btn.classList.add('has-representative');
          }
        });
      }
    })
    .catch(error => console.error('Error fetching provinces:', error));
});


/*******css*******/
/* برای اضافه کردن استایل به استان‌هایی که نماینده دارند */
path.has-representative {
    fill: #ffcc00; /* تغییر رنگ به زرد برای استان‌هایی که نماینده دارند */
}

button.has-representative {
    background-color: #ffcc00; /* تغییر رنگ دکمه به زرد */
}
